<template>
	<el-card shadow="hover" style="height:500px;margin-top: 10px;width: 100%;margin-left: 10px;overflow:scroll">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="100px"
			label-position="left">
			<el-form-item label="角色名称" prop="roleName">
				<el-input v-model="form.roleName" clearable></el-input>
			</el-form-item>
			<el-form-item label="角色编码" prop="roleCode">
				<el-input v-model="form.roleCode" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sort" controls-position="right" :min="1"
					style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="岗位" prop="position">
				<el-select v-model="form.position" clearable filterable placeholder="请选择岗位" :loading="postLoading"
					style="width: 100%;">
					<el-option v-for="item in postOptions" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="描述" prop="description">
				<el-input v-model="form.description" clearable type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="组织" prop="orgId">
				<el-input v-model="form.orgName" disabled></el-input>
				<!--				<el-cascader v-model="form.orgId" :options="groups" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>-->
			</el-form-item>
		</el-form>
	</el-card>
	<div style="width: 100%;height: 70px;position: absolute;bottom: 0;border-top: 0px solid #E8E8E8;padding-top: 10px">
		<el-button style="margin-left: 30px" @click="close()">取 消</el-button>
		<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
	</div>

</template>

<script>
export default {
	emits: ['success', 'closed'],

	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			postLoading: false,
			postOptions: [],
			//表单数据
			form: {
				roleName: "",
				roleCode: "",
				description: "",
				sort: "",
				// sort: 1,
				// status: 1,
				orgId: ""
			},
			//验证规则
			rules: {
				sort: [
					{ required: true, message: '请输入排序', trigger: 'change' }
				],
				roleName: [
					{ required: true, message: '请输入角色名称' }
				],
				roleCode: [
					{ required: true, message: '请输入角色编码' }
				],
				// position: [
				// 	{ required: true, message: '请选择岗位' }
				// ]
			},
			groups: [],
			groupsProps: {
				value: "id",
				emitPath: false,
				checkStrictly: true
			}
		}
	},
	async mounted() {
		this.getGroup()
		// this.getNumber()
		var res = await this.$API.emp.posnList.get({ data: {} })
		if (res && res.code == 0) {
			this.postOptions = res.data.map(item => {
				return { value: item, label: item };
			});
		}
	},
	methods: {
		//加载树数据
		async getGroup() {
			var res = await this.$API.user.dept.list.post({ data: {} });
			this.groups = res.data;
		},
		// 编码
		// async getNumber(){
		// 	var res = await this.$API.basic.role.getNumber.post();
		// 	if(res.code == 0){
		//           this.form.roleCode = res.data
		// 	}
		// 	// this.groups = res.data;
		// },
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.basic.role.add.post({ data: this.form });
					this.isSaveing = false;
					if (res && res.code == 0) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$emit('guanbi')
						this.$message.success("操作成功")
					}
				}
			})
		},
		close() {
			this.$emit('guanbi')
		},
		//表单注入数据
		setData(data) {
			this.form.orgId = data.id
			this.form.orgName = data.label
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style></style>
